﻿'use strict'


var toDoApp = {};
toDoApp.toDoListArray = [];

toDoApp.readToDo = function () {
    var t = '<table class="table table-bordered table-striped">';

    t += '<tr>';
    t += '<th>Priority</th>';
    t += '<th>To Do Task</th>';    
    t += '<th>Date Added</th>';
    t += '<th>Date Completed</th>';
    t += '</tr>';

    for (var index in this.toDoListArray) {
        var taskIn = this.toDoListArray[index];

        t += '<tr>';
        t += '<td>' + taskIn.priority + '</td>';
        t += '<td>' + taskIn.toDoTask + '</td>';        
        t += '<td>' + taskIn.startDate + '</td>';
        t += "<td><input type='button' id='complete' onclick='completeToDo(" + index + ")' value='Mark as Complete' />" + taskIn.finishDate + "<td>";
        t += '</tr>';
    }

    t += '</table>';

    this.toDoListArray.sort(function (a, b) {
        if (a.priority < b.priority) {
            return -1;
        } else if (a.priority > b.priority) {
            return 1;
        } else {
            return 0;
        }
    });

    document.getElementById('table').innerHTML = t;
};

toDoApp.completeToDo = function (i) {
    var finishDate = new Date();
    toDoApp.toDoListArray[i].push(finishDate);

    var request = new XMLHttpRequest();
    request.open('PUT', 'https://coralstodoapp.firebaseio.com/' + i + '/.json', false)
    request.onload = function () {
        if (this.status >= '200' && this.status < '400') {
            document.getElementById('complete').style.visibility = "hidden";

        }
        else {
            alert('Something failed!');
        }
    }
    var newDate = JSON.stringify(finishDate);
    request.send(newDate);
    toDoApp.readToDo();
}

toDoApp.submitTask = function () {
    var taskInput = document.getElementById('toDoItem');
    var priorityInput = document.getElementById('priorityLevel');
    
    var taskIn = this.toDoListArray;
    taskIn.toDoTask = taskInput.value;
    taskIn.priority = priorityInput.value;
    taskIn.startDate = new Date();

    var request = new XMLHttpRequest();
    request.open('POST', 'https://coralstodoapp.firebaseio.com/.json', false);
    request.onload = function () {
        if (this.status >= '200' && this.status < '400') {
            var data = JSON.parse(this.response);
            taskIn.id = data.name;

            toDoApp.toDoListArray.push(taskIn);            

            taskInput.value = priorityInput.value = " ";
        }
        else {
            alert('Something failed!');
        }
    }
    var newTaskAsStr = JSON.stringify(taskIn);

    request.send(newTaskAsStr);
    toDoApp.readToDo();
};

toDoApp.getTasks = function () {
    var request = new XMLHttpRequest();

    request.open('GET', 'https://coralstodoapp.firebaseio.com/.json', false);

    request.onload = function () {
        if (this.status >= "200" && this.status < "400") {
            var data = JSON.parse(this.response);

            toDoApp.toDoListArray.length = 0;

            for (var m in data) {
                var taskIn = data[m];
                taskIn.id = m;
                toDoApp.toDoListArray.push(taskIn);
            }

            toDoApp.readToDo();
        }
        else {
            document.getElementById('result').innerHTML = "Some error occured";
        }
    }

    request.send();
}();
